package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.BaseEntity;
import com.archstoday.entity.UserInfoEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;
import com.gifdialog.DialogUtil;
import com.gifdialog.GifDialog;

/**
 * 用户资料编辑页面-输入框编辑页面
 */
public class EditInfoActivity extends AsScrollBaseActivity {
    private TextView mTitleTv;
    private EditText mEdit;

    private boolean isNick;
    private String mEditStr;

    private GifDialog loadingDialog;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_edit_info);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mEdit = (EditText) findViewById(R.id.info_edit);
    }

    @Override
    protected void initData(Bundle bundle) {
        isNick = getIntent().getBooleanExtra("isNick", false);
        if (isNick) {
            mTitleTv.setText("用户名");
            mEdit.setHint("请输入用户名");
            mEdit.setText(OAuthUtils.getInstance().getUserInfo().getName());
        } else {
            mTitleTv.setText("年龄");
            mEdit.setHint("请输入年龄");
            mEdit.setText(String.valueOf(OAuthUtils.getInstance().getUserInfo().getAge()));
            mEdit.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.cancel_btn);
        setOnClickListener(R.id.submit_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.cancel_btn:
                appFinish();
                break;
            case R.id.submit_btn:
                mEditStr = mEdit.getText().toString();
                if (!StringUtil.isEmpty(mEditStr)) {
                    loadingDialog = new DialogUtil().showGifdialog(mContext);
                    loadingDialog.show();

                    modifyInfo();
                }
                break;
        }
    }

    private void modifyInfo() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        if (isNick) {
            mParams.put("name", mEditStr);
        } else {
            mParams.put("age", Integer.valueOf(mEditStr));
        }

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.MODIFY_USER_INFO),
                AsRes.MODIFY_USER_INFO, mParams, BaseEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }

        if (client.getUrlIdentifier() == AsRes.MODIFY_USER_INFO) {
            BaseEntity entity = (BaseEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (isNick) {
                    showToast("用户名修改成功");
                    OAuthUtils.getInstance().setNick(mEditStr);

                    UserInfoEntity.DataBean dataBean = OAuthUtils.getInstance().getUserInfo();
                    dataBean.setName(mEditStr);

                    OAuthUtils.getInstance().setUserInfo(dataBean);
                } else {
                    showToast("年龄修改成功");
                    OAuthUtils.getInstance().setAge(Integer.valueOf(mEditStr));

                    UserInfoEntity.DataBean dataBean = OAuthUtils.getInstance().getUserInfo();
                    dataBean.setAge(Integer.valueOf(mEditStr));

                    OAuthUtils.getInstance().setUserInfo(dataBean);
                }
                appFinish();
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            } else {
                showToast(entity.getMsg());
            }
        }
    }
}